/*
* jQuery's jqfaceedit Plugin
*
*  jquery face html edit
*  
*
* @author yuan lin
* @version 0.1
* @copyright Copyright(c) 2010.  
* @date 2010-10-11       
*/
jQuery.fn.jqfaceedit  = function(options) {
 
        var defaults = {
            txtAreaObj: options //TextArea\u7035\u7845\u8584
        }
        var options = $.extend(defaults, options);
 
        this.each(function(){
            var Obj = $(this);
            $(Obj).bind("click", function(e) {
            		var faceHtml = '<div id="message_face_menu" style="height:150px; width:500px;">1</div>';
                var faceHtml = '<div id="message_face_menu" class="facebox" style="position: absolute; display:none"><ul>';
                for (i = 20000; i < 20079; i++) {
           
                    faceHtml += '<li Other=' + i + '><img src="/images/emoticons/' + i + '.gif"  style="cursor:pointer; position:relative;"   /></li>';
                }
                faceHtml += '</ul></div>';
                var height = $("body").height();
 
                var width = $("body").width();
                faceHtml += '<div id="uchome_face_bg" style="position: absolute; top: 0px; left: 0px; width:' + width + 'px; height: ' + height + 'px; background-color: rgb(255, 255, 255); z-index: 10000; opacity: 0;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0,finishOpacity=100,style=0)"></div>';
                $("body").append(faceHtml);
                $("#uchome_face_bg").bind("click", function(e) {
                    $('#uchome_face_bg').remove();
                    $("#message_face_menu").css("display", "none");
 
                });
                $("#message_face_menu ul >li").bind("click", function() {
                    var id = $(this).attr("Other");
                    var faceText = '[em:' + id + ']';
 
                    $("#" + options.txtAreaObj).val($("#" + options.txtAreaObj).val() + faceText);
                    $("#message_face_menu").remove();
                    $("#uchome_face_bg").remove();
 
                    var setFocusText = $("#" + options.txtAreaObj);
                    var setFocusTextLeg = setFocusText.val().length;
                    setFocusText.focus(); // \u699b\u6a3f\ue17b\u6d63\u8de8\u6564focus\u93c2\u89c4\u7876\u9471\u6c31\u528d
                    // \u9352\u3086\u67c7\u93c4\ue21a\u60c1\u6d93\u7bd2e\u5a34\u5fda\ue787\u9363\ufffd
                    if ($.browser.msie) {
                        var txt = setFocusText[0].createTextRange(); // \u704f\u55d5\u7d36\u934f\u30e7\u6b91\u93ba\u0442\u6b22\u7035\u7845\u8584\u675e\ue101\u5d32\u6d93\u7bcbom\u7035\u7845\u8584\u951b\u5c7d\u82df\u9352\u6d98\u7f13\u6d93\ufffd\u91dcTextRange\u7035\u7845\u8584
                        txt.moveStart('character', setFocusTextLeg);   // \u7481\u5267\u7586\u934f\u590b\u7223\u93c4\u5267\u305a\u9428\u52ea\u7d85\u7f03\ufffd
                        txt.collapse(true);
                        txt.select();
                    }
                });
                var offset = $(e.target).offset();
                offset.top += $(this).height();
                $("#message_face_menu").css(offset).show();
            });
 
 
            $("#" + options.buttonObj).bind("click", function(e) {
                var rContent = $("#" + options.txtAreaObj).val();
                rContent = rContent.replace(/\[em:/g, '<img src=\'/images/emoticons/');
                rContent = rContent.replace(/\]/g, ".gif\' />");
                return rContent;
            });
        });
   };
    //\u7ec9\u4f79\u6e41\u9351\u82a5\u669f \u9422\u3124\u7c2chtml\u93c7\u630e\u5d32 
    function RepHtml(str) {
        str = str.replace(/\[em:/g, '<img src=\"/images/emoticons/');
        str = str.replace(/\]/g, ".gif\" ></img>");
        return str;
    };
 
    // \u7039\u6c2b\u7b9f\u93c6\u64ae\u6e76get html\u9351\u82a5\u669f   
    $.fn.jqfaceedit.Html = function(obj) {
        var rContent = $("#" + obj).val();
        rContent = RepHtml(rContent);
        return rContent;
    }
